/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.examination.web;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.mockexam.theory.user.entity.TmockexamTheoryUser;
import com.jeesite.modules.app.mockexam.theory.user.service.TmockexamTheoryUserService;
import com.jeesite.modules.app.util.time.DateUtils;
import com.jeesite.modules.zx.course.authorization.entity.TcourseAuthorization;
import com.jeesite.modules.zx.course.authorization.service.TcourseAuthorizationService;
import com.jeesite.modules.zx.course.entity.Tcourses;
import com.jeesite.modules.zx.course.service.TcoursesService;
import com.jeesite.modules.zx.course.study.entity.ZxCourseStudy;
import com.jeesite.modules.zx.course.study.service.ZxCourseStudyService;
import com.jeesite.modules.zx.examination.entity.Texamination;
import com.jeesite.modules.zx.examination.service.TexaminationService;
import com.jeesite.modules.zx.examination.vip.entity.TexaminationVip;
import com.jeesite.modules.zx.examination.vip.service.TexaminationVipService;
import com.jeesite.modules.zx.mockexam.theory.entity.TmockexamTheory;
import com.jeesite.modules.zx.mockexam.theory.service.TmockexamTheoryService;
import com.jeesite.modules.zx.user.vip.entity.TuserVip;
import com.jeesite.modules.zx.user.vip.service.TuserVipService;

/**
 * 专项模拟考试表Controller
 * @author zpp
 * @version 2020-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/examination/texamination")
public class TexaminationController extends BaseController {

	@Autowired
	private TexaminationService texaminationService;
	@Autowired
	private TexaminationVipService texaminationVipService;
	@Autowired
	private TuserVipService tuserVipService;
	@Autowired
	private TmockexamTheoryUserService tmockexamTheoryUserService;
	@Autowired
	private ZxCourseStudyService zxCourseStudyService;
	@Autowired
	private TcourseAuthorizationService tcourseAuthorizationService;
	@Autowired
	private TcoursesService tcoursesService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Texamination get(String id, boolean isNewRecord) {
		return texaminationService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:examination:texamination:view")
	@RequestMapping(value = {"list", ""})
	public String list(Texamination texamination, Model model) {
		model.addAttribute("texamination", texamination);
		return "modules/zx/examination/texaminationList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:examination:texamination:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Texamination> listData(Texamination texamination, HttpServletRequest request, HttpServletResponse response) {
		texamination.setPage(new Page<>(request, response));
		Page<Texamination> page = texaminationService.findPage(texamination);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:examination:texamination:view")
	@RequestMapping(value = "form")
	public String form(Texamination texamination, Model model) {
		model.addAttribute("texamination", texamination);
		return "modules/zx/examination/texaminationForm";
	}

	/**
	 * 保存专项模拟考试表
	 */
	@RequiresPermissions("zx:examination:texamination:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Texamination texamination) {
		Date signupStartTime = texamination.getSignupStartTime();
		LocalDateTime signupStartLocalDateTime = DateUtils.date2LocalDateTime(signupStartTime);
		Date signupEndTime = texamination.getSignupEndTime();
		LocalDateTime signupEndLocalDateTime = DateUtils.date2LocalDateTime(signupEndTime);
		LocalDateTime now=LocalDateTime.now();
		//1.判断考试报名是否已经结束
		Long betweenHours = DateUtils.betweenHours(now, signupEndLocalDateTime);
		if(betweenHours<=0){
			return renderResult(Global.FALSE, text("考试报名结束日期异常"));
		}
		//2.判断开始时间
		Long betweenHours2 = DateUtils.betweenHours(signupStartLocalDateTime,signupEndLocalDateTime);
		if(betweenHours2<=0){
			return renderResult(Global.FALSE, text("考试报名结束时间必须大于开始时间"));
		}
		//3.判断考场考试开始时间是否大于报名开始时间
		texaminationService.save(texamination);
		//创建考试添加会员
		TuserVip userVip=new TuserVip();
		userVip.getSqlMap().getWhere().and("vip_starttime", QueryType.LTE, now).and("vip_endtime", QueryType.GTE, now);
		List<TuserVip> findList = tuserVipService.findList(userVip);
		for(TuserVip tv:findList){
			String userId = tv.getUserId();
			TmockexamTheoryUser tmockexamTheory=new TmockexamTheoryUser();
			tmockexamTheory.setUserId(userId);
			tmockexamTheory.getSqlMap().getWhere().and("score", QueryType.GTE, "85");
			List<TmockexamTheoryUser> findList2 = tmockexamTheoryUserService.findList(tmockexamTheory);
			if(findList2!=null&&findList2.size()>5){
				
				TcourseAuthorization courAuth=new TcourseAuthorization();
				courAuth.setUserId(userId);
				List<TcourseAuthorization> findList3 = tcourseAuthorizationService.findList(courAuth);
				boolean finish=true;
				for(TcourseAuthorization tauth: findList3){
					Tcourses course=new Tcourses();
					String parentCode=tauth.getCourseId();
					course.setParentCode(parentCode);
					Long cou=tcoursesService.findCount(course);
					ZxCourseStudy courStudy=new ZxCourseStudy();
					courStudy.setFinish(1);
					courStudy.setCourseId(parentCode);
					Long finiCou=zxCourseStudyService.findCount(courStudy);
					if(cou!=finiCou){
						finish=false;
					}
				}
				if(finish){
					TexaminationVip texaminationVip=new TexaminationVip();
					texaminationVip.setExamId(texamination.getId());
					texaminationVip.setVipUser(userId);
					texaminationVipService.save(texaminationVip);
				}
			}
		}
		
		
		return renderResult(Global.TRUE, text("保存专项模拟考试表成功！"));
	}
	
	/**
	 * 停用专项模拟考试表
	 */
	@RequiresPermissions("zx:examination:texamination:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Texamination texamination) {
		texamination.setStatus(Texamination.STATUS_DISABLE);
		texaminationService.updateStatus(texamination);
		return renderResult(Global.TRUE, text("停用专项模拟考试表成功"));
	}
	
	/**
	 * 启用专项模拟考试表
	 */
	@RequiresPermissions("zx:examination:texamination:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Texamination texamination) {
		texamination.setStatus(Texamination.STATUS_NORMAL);
		texaminationService.updateStatus(texamination);
		return renderResult(Global.TRUE, text("启用专项模拟考试表成功"));
	}
	
	/**
	 * 删除专项模拟考试表
	 */
	@RequiresPermissions("zx:examination:texamination:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Texamination texamination) {
		texaminationService.delete(texamination);
		return renderResult(Global.TRUE, text("删除专项模拟考试表成功！"));
	}
	
}